// @ts-ignore
import { execSync } from 'child_process';
// @ts-ignore
import { writeFileSync, mkdirSync, existsSync } from 'fs';
// @ts-ignore
import { join } from 'path';

const huskyDir = join('.husky');
const preCommitFile = join(huskyDir, 'pre-commit');
const commitMsgFile = join(huskyDir, 'commit-msg');

// 初始化 husky
console.log('📦 初始化 Husky...');
execSync('npx husky init', { stdio: 'inherit' });

// 创建 pre-commit 钩子
console.log('✏️ 写入 pre-commit 钩子...');
writeFileSync(
  preCommitFile,
  `#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
`,
  { encoding: 'utf-8' },
);

// 创建 commit-msg 钩子
console.log('✏️ 写入 commit-msg 钩子...');
writeFileSync(
  commitMsgFile,
  `#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no -- commitlint --edit "$1"
`,
  { encoding: 'utf-8' },
);

console.log('✅ Husky 配置完成！');
